<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Gallery extends CI_Controller {
    
   	function __construct()
	{
		parent::__construct();
        $this->load->helper('html');
        $this->load->helper("url");
        $this->load->model('gallery_model');
        $this->load->library("pagination");
        $this->load->library('session');
	}

	public function index()
	{  
	    //pagination settings
        $config = array();
        $config['base_url'] = "http://www.jessicahk.com/mywayofsitting/gallery";
        $config['total_rows'] = $this->gallery_model->approved_images_count();
        $config['per_page'] = 6;
        $config["uri_segment"] = 2;
        
        //config for bootstrap pagination class integration
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['first_link'] = false;
        $config['last_link'] = false;
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['prev_link'] = '&laquo';
        $config['prev_tag_open'] = '<li class="prev">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&raquo';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        
        $this->pagination->initialize($config);
        
        $page = ($this->uri->segment(2) > 0) ? $this->uri->segment(2) : 0;
        $data['show_img'] = $this->gallery_model->get_approved_images($config['per_page'],$page);
        $data["links"] = $this->pagination->create_links();
        
        //print_r($data['total_img']);
        $this->load->view('gallery',$data);
	}
    
    public function accessToken($id, $name, $email)
    {   
        //pagination settings
        $config = array();
        $config['base_url'] = "http://www.jessicahk.com/mywayofsitting/gallery/accessToken/". $id .'/'. $name .'/'. $email;
        $config['total_rows'] = $this->gallery_model->approved_images_count();
        $config['per_page'] = 6;
        $config["uri_segment"] = 6;
        
        //$temp = iconv(mb_detect_encoding($name, mb_detect_order(), true), "UTF-8", $name);
        //print_r(urldecode($name));
        //exit();

        //config for bootstrap pagination class integration
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['first_link'] = false;
        $config['last_link'] = false;
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['prev_link'] = '&laquo';
        $config['prev_tag_open'] = '<li class="prev">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&raquo';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        
        $this->pagination->initialize($config);
        
        $page = ($this->uri->segment(6) > 0) ? $this->uri->segment(6) : 0;
        $data['show_img'] = $this->gallery_model->get_approved_images($config['per_page'],$page);
        $data["links"] = $this->pagination->create_links();
        
        //grab users' facebook data
        $data['fb_id'] = $id;
        $data['fb_name'] = $name;
        if(isset($email)){
            $data['fb_email'] = $email;
        } else {
            $data['fb_email'] = 'none';
        }
        
        //refresh the results and prevent the user keep voting the same image
        $data['voted_img'] = $this->gallery_model->get_all_voter_img($id); 
        
        //limit the number of votes to 5 
        $data['total_valid_votes'] = 5;
        
        //the valid votes left
        $data['total_voted_img'] = count($data['voted_img']);
        $data['valid_votes_left'] = $data['total_valid_votes'] - $data['total_voted_img'];
        
        //show successful msg 
        $data['success_msg'] = '';
        $data['success_msg'] = $this->session->flashdata('msg');
        if (!empty($data['success_msg'])){
            if($data['valid_votes_left'] == '0'){
                $data['success_msg'] = $data['success_msg'] . " " . "你已經用完所有投票機會。";
            } else{
                $data['success_msg'] = $data['success_msg'] . " " . "你還剩" . $data['valid_votes_left'] ."次投票機會。";
            }
        }
        
        $this->load->view('vote',$data);

   	} 
    
    public function voteImg($id, $name, $email, $currentPage, $img_id){
        //grab users' facebook data
        $data['fb_id'] = $id;
        $data['fb_name'] = str_replace("%20", " ", $name);
        
        //$temp = iconv(mb_detect_encoding($name, mb_detect_order(), true), "GB2312", $name);
        
        $data['fb_name'] = urldecode($data['fb_name']);
        //print_r($data['fb_name']);
        //exit();
        
        if(isset($email)){
            $data['fb_email'] = str_replace("-", "@", $email);
        } else {
            $data['fb_email'] = 'none';
        }
        
        //insert or update the voter's info
        $this->gallery_model->insert_vote_img($data['fb_id'],$data['fb_name'],$data['fb_email'],$img_id);
        
        //increase one vote for the $img_id
        $this->gallery_model->update_vote_results($img_id);
        
        //store successful messages in session
        $msg = "多謝支持！ 你已成功投票！";
        $this->session->set_flashdata('msg', $msg);
        
    
        if( $currentPage == '0'){
            redirect('http://www.jessicahk.com/mywayofsitting/gallery/accessToken/' .$id .'/'. $name .'/'. $email);
        } else {
            redirect('http://www.jessicahk.com/mywayofsitting/gallery/accessToken/' .$id .'/'. $name .'/'. $email .'/'. $currentPage);
        }
    }
        
}
